package 链表;

/**
 * https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/
 */
public class 剑指_Offer_24_反转链表 {


    public static class ListNode {
        int val;
        ListNode next;
        public ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }

        @Override
        public String toString() {
            return "ListNode{" +
                    "val=" + val +
                    ", next=" + next +
                    '}';
        }
    }

    public ListNode reverseList(ListNode head) {
        ListNode newHead = null;
        while (head != null) {
            ListNode node = head;
            head = node.next;
            node.next = newHead;
            newHead = node;
        }
        return newHead;
    }

    public static void main(String[] args) {
//        输入: 1->2->3->4->5->NULL
//        输出: 5->4->3->2->1->NULL

        ListNode node5 = new ListNode(5, null);
        ListNode node4 = new ListNode(4, node5);
        ListNode node3 = new ListNode(3, node4);
        ListNode node2 = new ListNode(2, node3);
        ListNode node1 = new ListNode(1, node2);

        System.out.println(new 剑指_Offer_24_反转链表().reverseList(node1));
    }

}
